import {
    BroadcastRequest,
    BroadcastResponse,
    BroadcastResponseStatus,
    BroadcastType,
    Controller,
    ControllerNo,
    GuiId,
    SpecialStoreKey
} from "./model/ControllerOpt";
import {ControllerBroadcast} from "./ControllerBroadcast";
import {
    AchievementSerializeBody,
    LIVERoomItem,
    RPGRoomItem,
    SerializeBody,
    SerializeBodyHistory,
    SerializeLoadType,
    StoreSerializeBody,
} from "./model/SerializeBody";
import {GlobalEnv} from "../utils/GlobalEnv";
import {AbilityController} from "./AbilityController";
import {AchievementController} from "./AchievementController";
import {TaskController} from "./TaskController";
import {UnitController} from "./UnitController";
import {Util} from "../utils/Util";
import {ColorStr, GameType, TaskApplyData} from "../type/Type";
import {GoodController} from "./GoodController";
import {ConfigKey} from "../constant/ConfigConstant";
import Configer from "../frame/apiModel/config/Configer";
import {gameTypePolicy} from "../policy/GameTypePolicy";
import {DataRouterUtil} from "../utils/router/DataRouterUtil";
import {Logger} from "../frame/apiModel/config/Logger";
import {PlayerModel} from "../frame/apiModel/adapterModel/model4/PlayerModel";
import {PlayerDataKey} from "../frame/dataSource/DataSource";
import {UnitJson} from "../frame/apiModel/model/StoreJson";
import {SyncDataKey, TriggerFactory} from "../frame/TriggerFactory";
import {GlobalModel} from "../frame/apiModel/adapterModel/model1/GlobalModel";
import {PlayerFactory} from "../frame/PlayerFactory";
import {CodeUtil} from "../frame/codeAdapter/CodeUtil";
import UnitUtil from "../utils/UnitUtil";


interface StoreSyncData {
    playerId: number,
    cno: ControllerNo,
    key: string,
    value: any,
}

export class StoreController implements Controller {
    getControllerNo(): number {
        return ControllerNo.StoreController;
    }
    private static storeController: StoreController = new StoreController();
    static getInstance(): StoreController {
        return StoreController.storeController;
    }

    init() {
        ControllerBroadcast.getInstance().startListen(StoreController.getInstance());
        TriggerFactory.getInstance().registerSyncData(SyncDataKey.history,  function (this: void, triggerPlayer, data: StoreSyncData) {
            const cno = data.cno;
            const playerId = data.playerId;
            const key = data.key;
            const value = data.value;
            StoreController.getInstance().setStoreHistory(playerId, cno, key, value)
        })
    }
    loadByJson(json: StoreSerializeBody, player: PlayerModel) {
        const storeTime = json.time;
        const currentTime = GlobalModel.getStartTime();
        Logger.toastProduct(`当前游戏时间 ${currentTime}`, player)
        if (storeTime) {
            Logger.toastProduct(`上次游戏时间 ${storeTime}`, player)
        }
        // 当保存的时间大于当前时间时
        if (!CodeUtil.isNullOrZero(currentTime)) {
            if (storeTime && currentTime > storeTime) {
                const isOneDay = Util.checkIsOneDay(currentTime, storeTime)
                if (isOneDay) {
                } else {
                    player.data[PlayerDataKey.oneDayCheck] = true;
                }
            }
        }

        json.time = currentTime;

        player.reflushUUId();

        let userId = json.userId;
        if (userId) {
            player.data[PlayerDataKey.userId] = userId;
        } else {
            userId = player.createUserId(currentTime, player.playerId);
            json.userId = userId;
        }

        player.data[PlayerDataKey.global] = json;


        let pop = json.pop;
        if (!CodeUtil.isNullOrZero(pop)) {
        } else {
            pop = 1;
        }
        const coin = json.coin;
        player.gold = coin;
        const wood = json.wood;
        player.lumber = wood;
        player.allFood = pop;
    }

    hasSave(player: PlayerModel) {
        const difficult = player.getStoredString(Util.getStoreKey('', 'time'));
        if (!CodeUtil.isNullOrZero(difficult)) {
            return true;
        }
        return false;
    }
    useStore(serializeBodys: SerializeBodyHistory, player: PlayerModel) {
        this.useStoreByControllerNo(serializeBodys, player, ControllerNo.UnitController);
        this.useStoreByControllerNo(serializeBodys, player, ControllerNo.GoodController);
        this.useStoreByControllerNo(serializeBodys, player, ControllerNo.AbilityController);
        this.useStoreByControllerNo(serializeBodys, player, ControllerNo.AchievementController);
        this.useStoreByControllerNo(serializeBodys, player, ControllerNo.TaskController);
        this.useStoreByControllerNo(serializeBodys, player, ControllerNo.StoreController);
    }
    useStoreByControllerNo(serializeBodys: SerializeBodyHistory, player:PlayerModel, no: ControllerNo) {
        switch (no) {
            case ControllerNo.UnitController: {
                const unitSerializeBody = serializeBodys[ControllerNo.UnitController];
                if (unitSerializeBody) {
                    UnitController.getInstance().loadByJson(unitSerializeBody, player);
                }
                break;
            }
            case ControllerNo.GoodController: {
                const goodsSerializeBody = serializeBodys[ControllerNo.GoodController];
                if (goodsSerializeBody) {
                    GoodController.getInstance().loadByJson(goodsSerializeBody, player);
                }
                break;
            }
            case ControllerNo.AbilityController:{
                const abilitySerializeBody = serializeBodys[ControllerNo.AbilityController];
                if (abilitySerializeBody) {
                    AbilityController.getInstance().loadByJson(abilitySerializeBody, player);
                }
                break;
            }
            case ControllerNo.AchievementController:{
                const achievementSerializeBody = serializeBodys[ControllerNo.AchievementController];
                if (achievementSerializeBody) {
                    AchievementController.getInstance().loadByJson(achievementSerializeBody, player);
                }
                break;
            }
            case ControllerNo.TaskController:{
                const taskSerializeBody = serializeBodys[ControllerNo.TaskController];
                if (taskSerializeBody) {
                    TaskController.getInstance().loadByJson(taskSerializeBody, player);
                }
                break;
            }
            case ControllerNo.StoreController:{
                const globalSerializeBody = serializeBodys[ControllerNo.StoreController];
                if (globalSerializeBody) {
                    StoreController.getInstance().loadByJson(globalSerializeBody, player);
                }
                break;
            }
        }
    }

    clearUnitStore(player: PlayerModel, archiveIndex: number) {
        const unitSerializeBody = player.data[PlayerDataKey.storeCurrent][ControllerNo.UnitController];
        const taskSerializeBody = player.data[PlayerDataKey.storeCurrent][ControllerNo.TaskController];
        const achSerializeBody = player.data[PlayerDataKey.storeCurrent][ControllerNo.AchievementController];
        const goodSerializeBody = player.data[PlayerDataKey.storeCurrent][ControllerNo.GoodController];
        const abilitySerializeBody = player.data[PlayerDataKey.storeCurrent][ControllerNo.AbilityController];


        UnitController.getInstance().mapUnitSerialize(archiveIndex, SerializeLoadType.current_archive, (key) => {
            unitSerializeBody[key] = '' as any;
        })

        TaskController.getInstance().mapTaskSerialize(archiveIndex, SerializeLoadType.current_archive, (key) => {
            taskSerializeBody[key] = '' as any;
        })

        AchievementController.getInstance().mapAchievementSerialize(archiveIndex, SerializeLoadType.current_archive, (key) => {
            achSerializeBody[key] = '' as any;
        })

        GoodController.getInstance().mapGoodSerialize(archiveIndex, SerializeLoadType.current_archive, (key) => {
            goodSerializeBody[key] = '' as any;
        })

        AbilityController.getInstance().mapAbilitySerialize(archiveIndex, SerializeLoadType.current_archive, (key) => {
            abilitySerializeBody[key] = '' as any;
        })

        const unitJsons: UnitJson[] = [];
        if (unitSerializeBody.hi1) {
            unitJsons[0] = unitSerializeBody.hi1;
        } else {
            unitJsons[0] = {} as any;
        }

        if (unitSerializeBody.hi2) {
            unitJsons[1] = unitSerializeBody.hi2;
        } else {
            unitJsons[1] = {} as any;
        }

        if (unitSerializeBody.hi3) {
            unitJsons[2] = unitSerializeBody.hi3;
        } else {
            unitJsons[2] = {} as any;
        }

        this.toSerialize(player, ControllerNo.UnitController, false);
        this.toSerialize(player, ControllerNo.TaskController, false);
        this.toSerialize(player, ControllerNo.AchievementController, false);
        this.toSerialize(player, ControllerNo.GoodController, false);
        this.toSerialize(player, ControllerNo.AbilityController, false);

        const request: BroadcastRequest = { broadcastType: BroadcastType.showUi, controllerNo: ControllerNo.GuiController, data: { player: player, guiId: GuiId.chooseArchiveBar, data: { unitJsons: unitJsons  } } }
        ControllerBroadcast.getInstance().broadcast(request);
    }
    getStore(player: PlayerModel, controllerNos: ControllerNo[], bClear: boolean = false): SerializeBodyHistory {
        if (bClear) {
            Logger.toastProduct(`英雄请重头再来!`, player)
        }
        let useKeyNum = 0;
        const storeHistory = player.data[PlayerDataKey.storeHistory];
        const playerId = player.playerId;
        for (let i = 0; i< controllerNos.length; i++) {
            const controllerNo = controllerNos[i];
            switch (controllerNo) {
                case ControllerNo.UnitController:{
                    UnitController.getInstance().mapUnitSerialize(0, SerializeLoadType.all_global, (key, index, type) => {
                        const value = player.getStoredString(Util.getStoreKey('', key));
                        if (!CodeUtil.isNullOrZero(value)) {
                            useKeyNum = useKeyNum + 1;
                            if (bClear) {
                                player.setStoredString(Util.getStoreKey('', key), '', false)
                            } else {
                                Logger.toastSave(`${key}: ${value}`, player)
                                const data: number = JSON.parse(value);
                                this.setStoreHistory(playerId, controllerNo, key, data)
                            }
                        }
                    })
                    break;
                }
                case ControllerNo.GoodController: {
                    GoodController.getInstance().mapGoodSerialize(0,  SerializeLoadType.all_global,(key, index, type) => {
                        const value = player.getStoredString(Util.getStoreKey('', key));
                        if (!CodeUtil.isNullOrZero(value)) {
                            useKeyNum = useKeyNum + 1;
                            if (bClear) {
                                player.setStoredString(Util.getStoreKey('', key), '', false)
                            } else {
                                Logger.toastSave(`${key}: ${value}`, player)
                                const data: number = JSON.parse(value);
                                this.setStoreHistory(playerId, controllerNo, key, data)
                            }
                        }
                    })
                    break;
                }
                case ControllerNo.AbilityController: {
                    AbilityController.getInstance().mapAbilitySerialize(0,  SerializeLoadType.all_global, (key, index, type) => {
                        const value = player.getStoredString(Util.getStoreKey('', key));
                        if (!CodeUtil.isNullOrZero(value)) {
                            useKeyNum = useKeyNum + 1;
                            if (bClear) {
                                player.setStoredString(Util.getStoreKey('', key), '', false)
                            } else {
                                Logger.toastSave(`${key}: ${value}`, player)
                                const data: number = JSON.parse(value);
                                this.setStoreHistory(playerId, controllerNo, key, data)
                            }
                        }
                    })
                    break;
                }
                case ControllerNo.AchievementController: {
                    AchievementController.getInstance().mapAchievementSerialize(0,  SerializeLoadType.all_global, (key, index, achId, type) => {
                        const value = player.getStoredString(Util.getStoreKey('', key));
                        if (!CodeUtil.isNullOrZero(value)) {
                            useKeyNum = useKeyNum + 1;
                            if (bClear) {
                                player.setStoredString(Util.getStoreKey('', key), '', false)
                            } else {
                                Logger.toastSave(`${key}: ${value}`, player)
                                const data: number = JSON.parse(value);
                                this.setStoreHistory(playerId, controllerNo, key, data)
                            }
                        }
                    })
                    break
                }
                case ControllerNo.TaskController: {
                    TaskController.getInstance().mapTaskSerialize(0, SerializeLoadType.all_global, (key, index, taskId) => {
                        const value = player.getStoredString(Util.getStoreKey('', key));
                        if (!CodeUtil.isNullOrZero(value)) {
                            useKeyNum = useKeyNum + 1;
                            if (bClear) {
                                player.setStoredString(Util.getStoreKey('', key), '', false)
                            } else {
                                Logger.toastSave(`${key}: ${value}`, player)
                                const data: TaskApplyData = JSON.parse(value);
                                this.setStoreHistory(playerId, controllerNo, key, data)
                            }
                        }
                    })
                    break;
                }
                case ControllerNo.StoreController: {
                    const globalSerializeBody: StoreSerializeBody = { diff: 0, time: 0, coin: 0, pop: 0, wood: 0,userId: '' };
                    const globalKeys = Object.keys(globalSerializeBody);
                    globalKeys.forEach(
                        (key, index, array) => {
                            const value = player.getStoredString(Util.getStoreKey('', key));
                            if (!CodeUtil.isNullOrZero(value)) {
                                useKeyNum = useKeyNum + 1;
                                if (bClear) {
                                    player.setStoredString(Util.getStoreKey('', key), '', false)
                                } else {
                                    Logger.toastSave(`${key}: ${value}`, player)
                                    const data: number | string = JSON.parse(value);
                                    this.setStoreHistory(playerId, controllerNo, key, data)
                                }
                            }
                        }
                    )
                    break;
                }
            }
        }
        Logger.toastProduct(`加载${useKeyNum}条数据`, player);
        const maxStoreNum = Configer.getConfig(ConfigKey.maxStoreNum);
        if (useKeyNum > maxStoreNum) {
            Logger.toastProduct(CodeUtil.getColorStr(ColorStr.red, `存档位数过多，请联系作者获取更多存档位!`), player);
        }
        return storeHistory;
    }
    getAchStoreHistory (player: PlayerModel): AchievementSerializeBody {
        const history = player.data[PlayerDataKey.storeHistory];
        return history[ControllerNo.AchievementController];
    }
    setStoreHistory (playerId: number, cno: ControllerNo, key: string, value: any) {
        const player = PlayerFactory.getInstance().getPlayer(playerId);
        const history = player.data[PlayerDataKey.storeHistory];
        history[cno][key] = JSON.parse(JSON.stringify(value));
        const current = player.data[PlayerDataKey.storeCurrent];
        current[cno][key] = JSON.parse(JSON.stringify(value));
    }
    // 保存全局变量
    serialize(player: PlayerModel): StoreSerializeBody {
        // Util.loggerInDev('保存', LogType.saveDev)
        // this.toSerialize(player, ControllerNo.AbilityController);
        // this.toSerialize(player, ControllerNo.AchievementController);
        // this.toSerialize(player, ControllerNo.UnitController);
        // this.toSerialize(player, ControllerNo.TaskController);
        // this.toSerialize(player, ControllerNo.GoodController);
        // this.toGlobalSerialize(player);
        const globals = player.data[PlayerDataKey.global];
        let body: StoreSerializeBody = JSON.parse(JSON.stringify(globals));
        if (body) {
        } else {
            // const arr = [];
            // const difficultDatas = DataRouter.getDifficultDatas();
            // const len = difficultDatas.length
            // for (let i = 0; i < len; i++) {
            //     arr[i] = 1
            // }
            body = { diff: 0, time: 0, coin: 0, pop: 0, wood: 0, userId: '' }
        }
        // const wave = GlobalEnv.wave;
        const difficult = GlobalEnv.difficult;
        // if (body.wave && body.wave[difficult] && body.wave[difficult] >= wave) {
        // } else {
        //     body.wave[difficult] = wave;
        // }
        let changeDiff = false;
        if (body.diff && body.diff >= difficult) {
        } else {
            if (GlobalEnv.getBeWin(player)) {
                body.diff = difficult;
                changeDiff = true;
            }
        }

        body.userId = player.data[PlayerDataKey.userId];

        // Util.formatTimeFormSecond()
        // 房间显示值

        const gameType = Configer.getConfig(ConfigKey.gameType);
        if (gameType === GameType.orpg) {

            const wood = player.lumber;
            const coin = player.gold;
            // const pop = GetPlayerState(player, PLAYER_STATE_RESOURCE_FOOD_CAP);
            const pop = 1;
            body.wood = wood;
            body.coin = coin;
            body.pop = pop;
            // const currentWave = body.wave[difficult];
            // const strWave = `${currentWave}`;
            const unit = GlobalEnv.getHero(player);
            const level =unit.level;
            const roomItem = player.data[PlayerDataKey.roomItemList] as RPGRoomItem;
            // if (currentWave !== roomItem.wave) {
            //     // 设置房间中项目
            //     if (player === GetLocalPlayer()) {
            //         DzAPI_Map_Stat_SetStat(player, 'wave', strWave);
            //     }
            //     roomItem.wave = currentWave;
            // }
            const attr = UnitUtil.getAttribute(unit);
            const fightValue = UnitUtil.getFightValueByAttr(attr?.newestAttribute);
            if (fightValue !== roomItem.fightValue) {
                const strFightValue = `${fightValue}`
                if (player.isLocalPlayer()) {
                    player.setRoomList('fightValue', strFightValue);
                }
                roomItem.fightValue = fightValue;
            }
            if (level !== roomItem.level) {
                const strLevel = `${level}`;
                if (player.isLocalPlayer()) {
                    player.setRoomList('level', strLevel);
                }
                roomItem.level = level;
            }
        } else if (gameType === GameType.live) {
            if (GlobalEnv.getBeWin(player)) {
                const roomItem = player.data[PlayerDataKey.roomItemList] as LIVERoomItem;
                const difficultData = DataRouterUtil.getDifficultData(body.diff);
                if (difficultData) {
                    const diffStr = difficultData.id;
                    if (changeDiff && diffStr !== roomItem.difficult) {
                        if (player.isLocalPlayer()) {
                            player.setRoomList('difficult', diffStr);
                        }
                        roomItem.difficult = diffStr;
                    }
                } else {
                    Logger.toastError(`玩家 ${player.name} 存档异常，可以联系作者排查`)
                }
            }
        }
        return body;
    }
    addStoreTask(player: PlayerModel, controllerNo: ControllerNo) {
        if (player?.isValid()) {
        } else {
            return;
        }
        // 白名单或者黑名单中都不会保存
        if (!CodeUtil.isNullOrZero(player.data[PlayerDataKey.bBlacklist]) || !CodeUtil.isNullOrZero(player.data[PlayerDataKey.bWhitelist])) {
            return;
        }

        const saveControllerNos = gameTypePolicy.getSaveControllerNos();
        let bSave = false;
        for (let i = 0; i < saveControllerNos.length; i++) {
            const no = saveControllerNos[i];
            if (no === controllerNo) {
                bSave = true;
                break;
            }
        }
        if (bSave) {
            const list = player.data[PlayerDataKey.storeList]
            if (list.indexOf(controllerNo) === -1) {
                list.push(controllerNo);
            }
        }
    }
    toWinStore(player: PlayerModel) {
        this.toEndStore(player, true);
    }
    toFaildStore(player: PlayerModel) {
        this.toEndStore(player, false);
    }
    toEndStore(player: PlayerModel, bWin: boolean) {
        const request: BroadcastRequest = { broadcastType: BroadcastType.getHandBookUnit, controllerNo: ControllerNo.UnitController, data: { player: player } }
        const res = ControllerBroadcast.getInstance().broadcast(request);
        const unitInfos: UnitJson[] = res.data.unitInfos;
        const achRequest: BroadcastRequest = { broadcastType: BroadcastType.achEndStore, controllerNo: ControllerNo.AchievementController, data: { player: player, unitInfos, bWin: bWin } }
        ControllerBroadcast.getInstance().broadcast(achRequest);
    }
    clearStoreTask(player: PlayerModel) {
        const list = player.data[PlayerDataKey.storeList];
        // Logger.toastSave(`保存队列:${JSON.stringify(list)}`, player)
        for (let i = 0; i < list.length; i ++) {
            const no = list[i];
            this.toSerialize(player, no, true)
        }
        list.length = 0;
        // Logger.toastSave(`保存成功,清空保存队列:${JSON.stringify(list)}`, player)
    }
    checkSerializeHistory (controllerNo: ControllerNo, serializeBody: SerializeBody, player: PlayerModel): string[] {
        // if (player.isLocalPlayer()) {
        // } else {
        //     return;
        // }
        // GlobalModel.bAsync = true;

        // const diffiKeys = [];
        const playerId = player.playerId;
        const history = player.data[PlayerDataKey.storeHistory];
        const oldSerialize = history[controllerNo];
        let newKeys: string[] = [];
        if (serializeBody) {
            newKeys = Object.keys(serializeBody);
        }
        Logger.toastSave(`keys: ${JSON.stringify(newKeys)}`, player);

        const bAch = (controllerNo === ControllerNo.AchievementController);

        if (oldSerialize) {
        } else {
            return newKeys;
        }
        for (let i = 0; i < newKeys.length; i ++) {
            const key = newKeys[i];

            // if (key === SpecialStoreKey.lockKey) {
            //     continue;
            // }

            const oldValue = oldSerialize[key];
            let oldStr = '';
            if (!CodeUtil.isNullOrZero(oldValue)) {
                oldStr = JSON.stringify(oldValue);
            }
            const newValue = serializeBody[key];
            let newStr = '';
            if (!CodeUtil.isNullOrZero(newValue)) {
                newStr = JSON.stringify(newValue);
            }
            if (oldStr !== newStr) {
                Logger.toastSave(`${key}: ${newStr}`, player)
                Logger.toastSave(`str.length: ${newStr.length}`, player)
                if (newStr.length > 60) {
                    Logger.toastError(`${key}: store value too long`);
                } else {

                    // 如果是成就控制器中的属性 需要保证防作弊锁的事务性
                    // if (bAch) {
                    //     player.setStoredString(Util.getStoreKey('', SpecialStoreKey.lockKey), '', false);
                    //     Logger.toastSave(`清空lock`, player)
                    // }

                    const ret = player.setStoredString(Util.getStoreKey('', key), newStr, true);

                    if (ret) {
                        this.setStoreHistory(player?.playerId, controllerNo, key, newValue);
                        // const storeSyncData: StoreSyncData = { playerId: playerId, cno: controllerNo, key: key, value: newValue };
                        // TriggerFactory.getInstance().SyncObjData(SyncDataKey.history, storeSyncData)
                    } else {
                        // Logger.toastError(`保存失败!`, player)
                    }

                    // if (bAch && ret) {
                    //     const lockKeyValue = serializeBody[SpecialStoreKey.lockKey];
                    //     let lockKeyStr = '';
                    //     if (lockKeyValue) {
                    //         lockKeyStr = JSON.stringify(lockKeyValue);
                    //         player.setStoredString(Util.getStoreKey('', SpecialStoreKey.lockKey), lockKeyStr, false);
                    //         Logger.toastSave(`更新lock：${lockKeyStr}`, player)
                    //         const storeSyncData: StoreSyncData = { playerId: playerId, cno: ControllerNo.AchievementController, key: SpecialStoreKey.lockKey, value: lockKeyStr };
                    //         TriggerFactory.getInstance().SyncObjData(SyncDataKey.history, storeSyncData)
                    //     }
                    // }
                }
                // diffiKeys.push(key);
            } else {
            }
        }
        // GlobalModel.bAsync = false;
        // SysToastUtil.toastSave(`不同的值: ${JSON.stringify(diffiKeys)}`, player)
    }
    toSerialize(player: PlayerModel, controllerNo: ControllerNo, reflushCurrent: boolean) {
        let serializeBody: SerializeBody;
        if (reflushCurrent) {
            const req: BroadcastRequest = { controllerNo, broadcastType: BroadcastType.serialize, data: { player } }
            const ret = ControllerBroadcast.getInstance().broadcast(req);
            if (ret.status === BroadcastResponseStatus.success) {
                serializeBody = ret.data.serializeBody;
            }
        } else {
            serializeBody = player.data[PlayerDataKey.storeCurrent][controllerNo];
        }
        if (serializeBody) {
            this.checkSerializeHistory(controllerNo, serializeBody, player);
        }
    }
    getMaxDifficult(player: PlayerModel): number {
        const storeHistory = player.data[PlayerDataKey.storeHistory]
        const difficult = storeHistory[ControllerNo.StoreController].diff;
        return difficult;
    }
    getOneDayCheck(player: PlayerModel): boolean {
        return player.data[PlayerDataKey.oneDayCheck];
    }
    getLastTime(player: PlayerModel): number {
        const storeHistory = player.data[PlayerDataKey.storeHistory]
        const time = storeHistory[ControllerNo.StoreController].time;
        return time;
    }
    // reflushLastTime(player: PlayerModel): boolean {
    //     const lastTime = this.getLastTime(player)
    //     const newTime = DzAPI_Map_GetGameStartTime();
    //     if (lastTime) {
    //         return true
    //     } else {
    //         Logger.toastProduct(`当前游戏时间: ${}`)
    //     }
    // }
    notify(request: BroadcastRequest): BroadcastResponse {
        const ret: BroadcastResponse = { controllerNo: this.getControllerNo(), data: {}, status: BroadcastResponseStatus.success };
        const type = request.broadcastType;
        const data = request.data;
        switch (type) {
            case BroadcastType.addStoreTask: {
                const player: PlayerModel = data.player;
                const no: ControllerNo = data.no;
                this.addStoreTask(player, no);
                break;
            }
            case BroadcastType.clearUnitStore: {
                const player: PlayerModel = data.player;
                const archiveIndex: number = data.archiveIndex;
                this.clearUnitStore(player, archiveIndex);
                break;
            }
            case BroadcastType.serialize: {
                const player: PlayerModel = data.player;
                const serializeBody: SerializeBody = this.serialize(player);
                ret.data = { serializeBody };
                break;
            }
            case BroadcastType.getMaxDifficult: {
                const player: PlayerModel = data.player;
                const difficult: number = this.getMaxDifficult(player);
                ret.data = { difficult };
                break;
            }
            case BroadcastType.getOneDayCheck: {
                const player: PlayerModel = data.player;
                const bOneDay: boolean = this.getOneDayCheck(player);
                ret.data = { bOneDay };
                break;
            }
            case BroadcastType.getLastTime: {
                const player: PlayerModel = data.player;
                const time: number = this.getLastTime(player);
                ret.data = { time };
                break;
            }
            case BroadcastType.toWinStore: {
                const player: PlayerModel = data.player;
                this.toWinStore(player);
                break;
            }
            case BroadcastType.toFaildStore: {
                const player: PlayerModel = data.player;
                this.toFaildStore(player);
                break;
            }
            case BroadcastType.getAchHistory: {
                const player: PlayerModel = data.player;
                const achHistory = this.getAchStoreHistory(player);
                ret.data = { achHistory };
                break;
            }
            default :
                ret.status = BroadcastResponseStatus.failed;
                break;
        }
        return ret;
    }

}